﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NomClasses;

namespace NomCafe
{
    public partial class frmPayTable : DevComponents.DotNetBar.Office2007RibbonForm
    {
        private Main_Cashier mainCashier;

        public frmPayTable(Main_Cashier mCashier)
        {
            InitializeComponent();
            mainCashier = mCashier;
        }

        private void frmPayTable_Load(object sender, EventArgs e)
        {
            panelEx1.Text = languageCashier.BILL + Lib_Cashier.currentNodeName + "(" + Lib_Cashier.currentRootName + ")";
            lblthungan.Text = Lib_Cashier.fullname;
            lblAddress.Text = Lib_Cashier.address;
            lblPhone.Text = Lib_Cashier.cafephone +" - " + languageCashier.FAX + Lib_Cashier.cafefax;
            lblTaxCode.Text = Lib_Cashier.cafetaxcode;
            lblnameCafe.Text = "<b><font size='+6'><i>Coffee</i><font color='#B02B2C'>" + Lib_Cashier.cafename + "</font></font></b>";
            getDataTable();
        }

        private void getDataTable()
        {
            NomDataContext nc = new NomDataContext();
            // Lấy bill_id của bàn đang chọn
            nom_bill bill_data = nc.nom_bills.Single(bill => bill.table_id == Main_Cashier.currenTable.id && bill.time_end == null);

            var bill_detail = from bill_de in nc.nom_bill_details
                              where bill_de.bill_id == bill_data.id
                              select bill_de;
            decimal tongtien = 0;
            foreach (var itemOnBill in bill_detail)
            {
                tongtien = itemOnBill.cash + tongtien;
                string[] row = {itemOnBill.quantity.ToString(), // số lượng
                                   itemOnBill.specifications.ToString(), // Quy cách    
                                   String.Format("{0:0,0}",itemOnBill.price), // Đơn giá
                                   String.Format("{0:0,0}",itemOnBill.cash), // Thành tiền
                                   itemOnBill.group_table.ToString(), // Mã bàn
                                   itemOnBill.note}; // Ghi chú

                listViewTableSelectItem.Items.Add(itemOnBill.nom_item.name).SubItems.AddRange(row);
                lblmasophieu.Text = itemOnBill.bill_id.ToString();
                lblmaban.Text = itemOnBill.nom_bill.nom_table.code;
                lblngay.Text = DateTime.Now.ToShortDateString();
                lblthoigian.Text = itemOnBill.nom_bill.time_start.Value.ToShortTimeString();
                lblketthuc.Text = DateTime.Now.ToShortTimeString();
            }
            lbltotal.Text = String.Format("{0:0,0}",tongtien);
        }

        private void btnPayTable_Click(object sender, EventArgs e)
        {
            NomDataContext nc = new NomDataContext();
            nom_bill billData = nc.nom_bills.Single(b => b.table_id == Main_Cashier.currenTable.id && b.time_end == null);
            billData.total =  Convert.ToDecimal(lbltotal.Text);
            billData.time_end = DateTime.Now;

            // xóa món ăn trên bàn thanh toán
            var billDetail = from bill_de in nc.nom_bill_details
                             where bill_de.bill_id == billData.id
                             select bill_de;
            nc.nom_bill_details.DeleteAllOnSubmit(billDetail);
            nc.SubmitChanges();

            // chuyển thành bàn trống
            mainCashier.setUnselectTable();
            this.Close();
        }

        private void btnCloseWindows_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
